package com.jiyinit.web.core.business.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.web.core.business.dto.FunRolePageRequest;
import com.jiyinit.web.core.business.dto.FunctionPageRequest;
import com.jiyinit.web.core.business.enums.ResourceTypeEnum;
import com.jiyinit.web.core.business.service.OrgFunctionServiceImpl;
import com.jiyinit.web.core.business.service.OrgServiceImpl;
import com.jiyinit.web.core.business.vo.FunctionRoleVo;
import com.jiyinit.web.core.business.vo.FunctionVo;
import com.jiyinit.web.core.common.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lr on 2017/12/25.
 */
@Controller
@RequestMapping( "/orgFun")
public class OrgFunctionController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OrgFunctionController.class);

    @Resource( name = "core.orgFunServiceImpl")
    private OrgFunctionServiceImpl orgFunctionService;

    @Resource(name = "core.orgServiceImpl")
    private OrgServiceImpl orgServiceImpl;

    @RequestMapping("/list")
    public String toList(){
        return "function/org/list";
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST,value = "/findByPage")
    public PagingResult<FunctionVo> findByPage( FunctionPageRequest functionPageRequest){
        LOGGER.info("params : {}"+ functionPageRequest);
        Integer orgId =  UserUtil.getCurrentUser().getOrgId();
        return orgFunctionService.findByPage(orgId,functionPageRequest);
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model){
        Integer orgId = UserUtil.getCurrentUser().getOrgId();
        Integer parentId = orgServiceImpl.getTopParentOrgId(orgId);
        model.addAttribute("orgId",parentId);
        return "function/org/update";
    }

    @ResponseBody
    @RequestMapping("anon/findFunction")
    public ResponseEntity<ResponseDto> findFunction(Integer orgId){
        List<FunctionVo> list = orgFunctionService.findFunction(orgId);
        List items = new ArrayList<>();
        for(FunctionVo fun : list){
            Map<String, Object> map = new HashMap<>();
            map.put("id", fun.getFunId());
            map.put("pId", fun.getParentId());
            map.put("name", fun.getFunName());
            if(fun.getNum() != null && fun.getNum() >0){
                map.put("open", true);
            } else {
                map.put("open", false);
            }
            if(!Check.isNullObj(fun.getId())){
                map.put("checked", true);
            }
            items.add(map);
        }
        return new ResponseEntity<>(ResponseDto.responseOK(items), HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("update")
    public ResponseEntity<ResponseDto> update(Integer orgId,Integer[] funIds){
        if(funIds == null){
            return new ResponseEntity<>(ResponseDto.responseFail("权限ID不能为空！"),HttpStatus.OK);
        }
        int num = orgFunctionService.update(orgId,funIds);
        if(num >0)
            return new ResponseEntity<>(ResponseDto.responseOK(""),HttpStatus.OK);
        return new ResponseEntity<>(ResponseDto.responseFail("失败！"),HttpStatus.OK);
    }
}
